<template>
  <BasicTable @register="registerTable">
    <template #toolbar>
      <a-button type="primary" @click="handleSave"> 新增</a-button>
    </template>
    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'action'">
        <TableAction
          stopButtonPropagation
          :actions="[
            {
              label: '详情',
              icon: 'clarity:info-standard-line',
              onClick: handleDesc.bind(null, record),
            },
            {
              label: '编辑',
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '删除',
              color: 'error',
              icon: 'ic:outline-delete-outline',
              popConfirm: {
                title: '确认删除？',
                okText: '确认',
                cancelText: '取消',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </template>
  </BasicTable>
  <LectureNoteDesc @register="registerDescModal" />
  <LectureNoteEdit @register="registerEditModal" />
</template>
<script lang="ts" setup>
  import LectureNoteEdit from "./Edit.vue";
  import { useModal } from "/@/components/Modal";
  import { useMessage } from "/@/hooks/web/useMessage";
  import { defaultActionColumn, defaultTableSettings } from "/@/hooks/core/useDefault";
  import { BasicTable, TableAction, useTable } from "/@/components/Table";
  import LectureNoteDesc from "/@/views/default/practical/LectureNoteDesc.vue";
  import { lectureNoteBasicColumns } from "/@/views/default/practical/practical.data";
  import { getLectureNotePageApi, removeLectureNoteApi } from "/@/api/core/lectureNote";
  import { OperTypeEnum } from "/@/enums/typeEnum";

  const [registerTable] = useTable({
    title: "实习听课记录列表",
    rowKey: "noteId",
    api: getLectureNotePageApi,
    columns: lectureNoteBasicColumns,
    actionColumn: {
      width: 240,
      ...defaultActionColumn,
    },
    ...defaultTableSettings,
  });

  const [registerDescModal, { openModal: openDescModal }] = useModal();
  const [registerEditModal, { openModal: openEditModal }] = useModal();

  function handleDesc(data) {
    openDescModal(true, data);
  }

  function handleEdit(data) {
    openEditModal(true, { data, operType: OperTypeEnum.UPDATE });
  }

  function handleSave() {
    openEditModal(true, { operType: OperTypeEnum.SAVE });
  }

  const { createMessage } = useMessage();

  async function handleDelete(data) {
    await removeLectureNoteApi(data.noteId);
    createMessage.success("删除听课记录成功");
  }
</script>
